package org.csu.jpetstorespringversion.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.jpetstorespringversion.mapper.InventoryMapper;
import org.csu.jpetstorespringversion.mapper.ItemMapper;
import org.csu.jpetstorespringversion.model.*;
import org.csu.jpetstorespringversion.service.ItemService;
import org.csu.jpetstorespringversion.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {
    @Autowired
    Item item;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    InventoryMapper inventoryMapper;
    @Override
    public void updateInventoryQuantity(Map<String, String> param) {

        String itemId = param.keySet().iterator().next();
       item.setItemId(itemId);
       int item1 =itemMapper.updateById(item);
        System.out.println(item1);
    }

    @Override
    public int getInventoryQuantity(String itemId) {
        QueryWrapper<Inventory> wrapper = new QueryWrapper<>();
        wrapper.eq("itemid",itemId);
        Inventory inventory1= inventoryMapper.selectOne(wrapper);
        return inventory1.getQty();
    }

    @Override
    public List<Item> getItemListByProduct(String productId) {
        List<Item> ItemList = new ArrayList<Item>();
        HashMap<String, Object> map=new HashMap<>();
        map.put("productid",productId);
        List<Item> items=itemMapper.selectByMap(map);
        return items;
    }

    @Override
    public Item getItem(String itemId) {
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        wrapper.eq("itemid",itemId);
        Item item1= itemMapper.selectOne(wrapper);
        //根据productID设置item的product属性
        ProductServiceImpl productService = new ProductServiceImpl();
        Product product=productService.getProduct(item.getProductId());
        item1.setProduct(product);
        System.out.println("item的productId是："+item.getProduct().getProductId());
        return item1;

    }
//    @Override
//    public Page<Item> itemlist(Item item, long current, long size) {
//        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
//        if (null != item.getItemid()) {
//            queryWrapper.eq("itemid", item.getItemid());
//            queryWrapper.like("productid", item.getProductid());
//            queryWrapper.like("listprice", item.getListprice());
//            queryWrapper.like("attr1", item.getAttr1());
//            queryWrapper.like("attr2", item.getAttr2());
//            queryWrapper.like("attr3", item.getAttr3());
//            queryWrapper.like("attr4", item.getAttr4());
//            queryWrapper.like("attr5", item.getAttr5());
//        }

//        return baseMapper.selectPage(new Page<>(current, size), queryWrapper);
//    }
}
